package com.shujia.mkdata

import java.util.Properties

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

import scala.io.Source

object MakeCarData {
  def main(args: Array[String]): Unit = {
    /**
      * 读取cars.json文件将数据实时写入kafka
      *
      */

    //1、读取文件
    val cars: Iterator[String] = Source.fromFile("data/cars.json").getLines()


    val properties = new Properties

    //1、指定kafkabroker地址
    properties.setProperty("bootstrap.servers", "master:9092")

    //2、指定kv的序列化类
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")


    //创建kafka生产者
    val producer = new KafkaProducer[String,String](properties)


    /**
      * kafka-topics.sh --create --zookeeper master:2181 --replication-factor 1 --partitions 3 --topic cars
      *
      */

    //2、循环将数据写入kafka
    for (car <- cars) {

      //构建一行数据
      val record = new ProducerRecord[String,String]("cars",car)

      //将数据写入kafka
      producer.send(record)
      producer.flush()

      println(car)
      Thread.sleep(50)
    }

    producer.close()
  }

}
